package com.example.easybuy.controller;

import com.example.easybuy.entity.Like;
import com.example.easybuy.service.LikeService;
import com.example.easybuy.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("like")
public class LikeController {
    @Autowired
    private LikeService likeService;

    @RequestMapping("/addLike")
    public AjaxResult addLike(Like like, String loginName) {
        return likeService.addCollection(like, loginName);
    }

    @RequestMapping("/delLike")
    public AjaxResult delLike(int productId, String loginName) {
        if (likeService.delLike(productId, loginName)) {
            return AjaxResult.success().add("msg", "删除成功");
        }
        return AjaxResult.error().add("msg", "删除失败");
    }

    @RequestMapping("/getLikes")
    public AjaxResult getLikes(String loginName) {
        List<Like> likes = likeService.getLikes(loginName);
        return AjaxResult.success().add("likes", likes);
    }

    @RequestMapping("/delAll")
    public AjaxResult delAll(String loginName) {
        boolean isDel = likeService.delAll(loginName);
        return AjaxResult.success().add("isDel", isDel);
    }
}
